<?php
/**
 * Created by PhpStorm.
 * User: 曹旺
 * Date: 2018-05-21
 * Time: 14:00
 */
namespace app\index\model;
use think\Model;
use traits\model\SoftDelete;

class Exigency extends Model{
    // 设置完整的数据表（全称包含前缀）
    //protected $table = 'oa_exigency';
    //protected $autoWriteTimestamp = true;
    use SoftDelete;

    /**
     * @return \think\model\relation\HasOne
     * 关联查询单据类型
     * @author  曹旺
     * @data  17:09
     */
    public function typeapprove(){
        return $this->hasOne('TypeApprove','id','type_approve_id');
    }
    /**
     * @param $where
     * @param $page
     * @param $num
     * @return array
     * 申请数据列表数据接口
     * @author  曹旺
     * @data  15:07
     */
    public function exigency_approve_list_table($where,$page,$num){
        $res=$this->field('id,creat_time,apply_state')->where($where)->page($page, $num)->order('id DESC')->select()->toArray();
        foreach ($res as $key => $value) {
            //数据类型（）
            /*$type_approve = Db::table('oa_type_approve')->field('type_approve')->where(['id' => $value['bill_type_id']])->find();
            $res[$key]['type_approve'] = $type_approve['type_approve'];*/
            //审批状态（数据库存的json类型）
            $role = json_decode($value['apply_state'], true);
            $count_role_state = count($role) - 1;

            if ($role[$count_role_state]['state'] == 0) {
                $role_res=model('Role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find()->toArray();
                //$role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: #0096e8">审批中...</span>';
            }
            if ($role[$count_role_state]['state'] == 1) {
                $role_res=model('Role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find()->toArray();
                $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: black">申请被审批</span>';
            }
            if ($role[$count_role_state]['state'] == 2) {
                $role_res=model('Role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find()->toArray();
                $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: red">审批被驳回</span>';
            }
        }
        //数据个数
        $count=$this->where('state=1 and exigency_user_id=' . $_SESSION['id'])->count();
        //layui接口格式
        $data = ['code' => 0, 'msg' => '', 'count' => $count];
        $data['data'] = $res;
        return $data;
    }

    /**
     * @return array
     * 工程审批审批流程数据处理
     * @author  曹旺
     * @data   15:17
     */
    public function flow_exigency_apply($id='')
    {
        //审批流程数据处理
        session_start();
        //$ch_name = $_SESSION['en_name'];
        //$ch_name = '测试';
        $user_id = $_SESSION['id'];
        //$user_id = 66;
        $res = User::get($user_id);
        $bill_type_id = 10;
        $FlowModel = new Flow();
        if ($id!=''){
            $where = 'id='.$id." and bill_type_id=" . $bill_type_id . " and ( ( region_id=" . $res['region_id']
                . " and ( branch_id=" . $res['branch_id'] . " or branch_id =1 ) ) 
                or (( region_id = 2 ) and ( branch_id = 1 or branch_id= " . $res['branch_id'] . ")) )";
        }else{
            $where = "bill_type_id=" . $bill_type_id . " and ( ( region_id=" . $res['region_id']
                . " and ( branch_id=" . $res['branch_id'] . " or branch_id =1 ) ) 
                or (( region_id = 2 ) and ( branch_id = 1 or branch_id= " . $res['branch_id'] . ")) )";

        }
        $flow = $FlowModel->where($where)->select()->toArray();
        $RoleModel = new Role();
        foreach ($flow as $key => $value) {
            if (!empty($value['first'])) {
                $first = $RoleModel->where('id=' . $value['first'])->find();
                $flow[$key]['first'] = $first['role'];
                $flow[$key]['flow'] = $first['role'];
            } else {
                unset($flow[$key]['first']);
            }
            if (!empty($value['second'])) {
                $second = $RoleModel->where('id=' . $value['second'])->find();
                $flow[$key]['second'] = $second['role'];
                $flow[$key]['flow'] .= '->' . $second['role'];
            } else {
                unset($flow[$key]['second']);
            }
            if (!empty($value['third'])) {
                $third = $RoleModel->where('id=' . $value['third'])->find();
                $flow[$key]['third'] = $third['role'];
                $flow[$key]['flow'] .= '->' . $third['role'];
            } else {
                unset($flow[$key]['third']);
            }
            if (!empty($value['fourth'])) {
                $fourth = $RoleModel->where('id=' . $value['fourth'])->find();
                $flow[$key]['fourth'] = $fourth['role'];
                $flow[$key]['flow'] .= '->' . $fourth['role'];
            } else {
                unset($flow[$key]['fourth']);
            }
            if (!empty($value['fifth'])) {
                $fifth = $RoleModel->where('id=' . $value['fifth'])->find();
                $flow[$key]['fifth'] = $fifth['role'];
                $flow[$key]['flow'] .= '->' . $fifth['role'];
            } else {
                unset($flow[$key]['fifth']);
            }
            if (!empty($value['sixth'])) {
                $sixth = $RoleModel->where('id=' . $value['sixth'])->find();
                $flow[$key]['sixth'] = $sixth['role'];
                $flow[$key]['flow'] .= '->' . $sixth['role'];
            } else {
                unset($flow[$key]['sixth']);
            }
            if (!empty($value['seventh'])) {
                $seventh = $RoleModel->where('id=' . $value['seventh'])->find();
                $flow[$key]['seventh'] = $seventh['role'];
                $flow[$key]['flow'] .= '->' . $seventh['role'];
            } else {
                unset($flow[$key]['seventh']);
            }
            if (!empty($value['eighth'])) {
                $eighth = $RoleModel->where('id=' . $value['eighth'])->find();
                $flow[$key]['eighth'] = $eighth['role'];
                $flow[$key]['flow'] .= '->' . $eighth['role'];
            } else {
                unset($flow[$key]['eighth']);
            }
            if (!empty($value['ninth'])) {
                $ninth = $RoleModel->where('id=' . $value['ninth'])->find();
                $flow[$key]['ninth'] = $ninth['role'];
                $flow[$key]['flow'] .= '->' . $ninth['role'];
            } else {
                unset($flow[$key]['ninth']);
            }
            if (!empty($value['tenth'])) {
                $tenth = $RoleModel->where('id=' . $value['tenth'])->find();
                $flow[$key]['tenth'] = $tenth['role'];
                $flow[$key]['flow'] .= '->' . $tenth['role'];
            } else {
                unset($flow[$key]['tenth']);
            }
        }
        return $flow;
    }

    /**
     * @param $data工程进度的相关数据 （数组）
     * @param $id为user_id
     * @return false|int
     * 添加工程进度的申请的数据处理
     * @author  曹旺
     * @data  14:14
     */
    public function exigency_apply_sub($data, $id)
    {
        $flow = Flow::get($data['exigency_flow_id']);
        //return $flow->first;
        $role = Role::get($flow->first);
        //return $role->first;
        $Array['role'] = $role->id;
        $Array['role_key'] = 'first';
        $Array['user_id'] = '';
        //audit_opinion审核人的审核意见;
        $Array['audit_opinion'] = '';
        //审核时间
        $Array['audit_time'] = '';
        //审核人的名字
        $Array['audit_name'] = '';
        //0待审核1审核通过2驳回
        $Array['state'] = 0;
        $Arrays[]=$Array;
        $data['apply_state'] = json_encode($Arrays);
        $data['exigency_user_id'] = $id;
        //$data['apply_success']=0;
        $res = $this->save($data);
        return $res;
    }

    /**
     * @param $where
     * @param $page
     * @param $num
     * @return array
     * 审批数据列表数据
     * @author  曹旺
     * @data  09:54
     */
    public function exigency_apply_list($where,$id)
    {
        $num = 0;
        $states = '';
        $user_id = $_SESSION['id'];
        //我拥有的角色
        $RoleModel = new UserRole();
        $role=$RoleModel->where('user_id='.$user_id)->with('role')->where('state=1')->select()->toArray();
        //dump($role);
        foreach ($role as $key => $value) {
            //需要该角色审批的数据
            //$ProjectScheduleModel=new ProjectSchedule();
            if (isset($id)&& $id!=''){
                $apply_array=$this->with('typeapprove')->where($where)->where('id='.$id)->order('id DESC')->select()->toArray();
            }else{
                $apply_array=$this->with('typeapprove')->where($where)->order('id DESC')->select()->toArray();
            }
            //return $apply_array;
            //这是一条数据（表：紧急事件）
            foreach ($apply_array as $k => $v) {
                //dump($v);die;
                //转化审批状态的数据类型（转成数组）
                $apply = json_decode($v['apply_state'], true);
                //dump($apply);die;
                foreach ($apply as $kk => $vv) {
                    //dump($apply);die;
                    if ($vv['role'] == $value['role_id']) {
                        if ($vv['state'] == 0) {
                            $state = '<b>' . $value['role']['role'] . '</b>：<span style="color: #0096e8">未审批</span>';
                            $num++;
                        } elseif ($vv['state'] == 1) {
                            $state = '<b>' . $value['role']['role'] . '</b>：<span style="color: black">已审批</span>';
                        } else {
                            $state = '<b>' . $value['role']['role'] . '</b>：<span style="color: red">已驳回</span>';
                        }
                        $state_a['id'] = $v['id'];
                        $state_a['apply_state'] = $state;
                        $state_a['creat_time'] = $v['creat_time'];
                        $state_a['role'] = $vv['role'];
                        if (!empty($vv['user_id'])) {
                            $state_a['user_id'] = $vv['user_id'];
                        } else {
                            $state_a['user_id'] = '';
                        }
                        $state_a['role_key'] = $vv['role_key'];
                        $states[] = $state_a;
                    }
                }
            }
        }
        /*//$states=sort($states);
        //$num 暂时没用到
        $expense_reimbursement['num'] = $num;
        $expense_reimbursement['expense_reimbursement'] = $states;
        //sort($expense_reimbursement);
        //dump($expense_reimbursement);die;*/
        return $states;
    }
}